package com.tang.lts.core.biz.logger.mysql;

import com.tang.lts.core.biz.logger.JobLogger;
import com.tang.lts.core.biz.logger.domain.JobLogPo;
import com.tang.lts.core.cluster.Config;
import com.tang.lts.core.store.JdbcAbstractAccess;
import com.tang.lts.core.store.builder.InsertSql;

import java.util.List;

/**
 * @author Tang
 * @classname MySqlJobLogger
 * @description [ MySqlJobLogger mysql 存储方式，继承JdbcAbstractAccess获取操作db的能力  ]
 * @date 2022/5/8 12:44
 */
public class MySqlJobLogger extends JdbcAbstractAccess implements JobLogger {

    public MySqlJobLogger(Config config) {
        super(config);
    }

    @Override
    public void log(JobLogPo jobLogPo) {
        InsertSql insertSql = buildInsertSql();
        setInsertSqlValues(insertSql, jobLogPo);
        insertSql.doInsert();
    }

    @Override
    public void log(List<JobLogPo> jobLogPos) {
        for (JobLogPo jobLogPo : jobLogPos) {

        }
    }

    private InsertSql buildInsertSql() {
        return new InsertSql(getSqlTemplate())
                .insert(getTableName())
                .columns("log_time",
                        "gmt_created",
                        "log_type",
                        "success",
                        "msg"
                )
                ;
    }

    private void setInsertSqlValues(InsertSql insertSql,JobLogPo jobLogPo) {
        insertSql.values(
                jobLogPo.getGmtCreated(),
                jobLogPo.getLogTime(),
                jobLogPo.isSuccess()
        );
    }

    private String getTableName() {
        return "lts_job_log_po";
    }

}